package wiki.hitime.demo.test.service;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import wiki.hitime.demo.test.bean.db.Order;
import wiki.hitime.demo.test.bean.dto.OrderDTO;
import wiki.hitime.demo.test.bean.dto.ResultDTO;
import wiki.hitime.demo.test.enums.OrderTypeEnum;
import wiki.hitime.demo.test.enums.ResponseCodeEnum;
import wiki.hitime.demo.test.manager.OrderManager;

import javax.annotation.Resource;

/**
 * @author zhangdaoping
 * @create 2019-10-30 16:53
 */
@Service
public class NestedBusinessService {

    @Resource
    private OrderManager orderManager;

    public ResultDTO process(String orderId) {
        Order order = orderManager.loadByOrderId(orderId);
        if (order == null) {
            return ResultDTO.response(ResponseCodeEnum.NO_ORDER);
        }
        OrderTypeEnum orderTypeEnum = OrderTypeEnum.getEnumByStatus(order.getOrderType());
        Assert.notNull(orderTypeEnum, "订单类型不存在");
        switch (orderTypeEnum) {
            case NORMAL: {
                // 正常数据处理
            }
            break;
            case FAST: {
                // 快审数据处理
            }
            break;
            default:
        }
        return ResultDTO.success(convertDTO(order));
    }


    private OrderDTO convertDTO(Order source) {
        // 加载订单类型名称
        OrderTypeEnum orderTypeEnum = OrderTypeEnum.getEnumByStatus(source.getOrderType());
        Assert.notNull(orderTypeEnum, "订单类型不存在");
        return OrderDTO.builder()
                .orderId(source.getOrderId())
                .name(source.getName())
                .orderType(source.getOrderType())
                .orderTypeName(orderTypeEnum.getMsg())
                .build();
    }
}
